/*
 * Created by JFormDesigner on Sun Oct 19 16:56:12 CST 2025
 */

package com.geek.javase.ch10.jformdesigntest.event;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 27636
 */
public class EventDemo3 extends JFrame {
    public static void main(String[] args) {
        new EventDemo3().setVisible(true);
    }
    public EventDemo3() {
        /*//设置标题
        this.setTitle("这是事件案例");*/
        this.setTitle("事件");
        //设置大小
        this.setSize(400,450);
        /*//设置关闭方式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
        //初始化容器
        initComponents();
    }
    //键盘按下
    private void textFieldKeyPressed(KeyEvent e) {
        // TODO add your code here
        System.out.println("按键字符：" + e.getKeyChar());

    }
    //键盘释放
    private void textFieldKeyReleased(KeyEvent e) {
        // TODO add your code here
        System.out.println("按键Code："+e.getKeyCode());
        if(e.getKeyCode() == 10) {
            String text = textField.getText();
            button.setText(text);
        }
    }
    //鼠标点击
    private void buttonMouseClicked(MouseEvent e) {
        // TODO add your code here
        int button = e.getButton();
        if(button == 1) {
            System.out.println("左键");
            System.out.println("左键点击次数：" +e.getClickCount());
        } else if (button == 2) {
            System.out.println("中键");
            System.out.println("中键点击次数：" +e.getClickCount());
        } else if (button == 3) {
            System.out.println("右键");
            System.out.println("右键点击次数：" +e.getClickCount());

        }
    }

    //这里的是默认固定的，修改也会重新复原原始代码。因此，修改的部分会造成丢失
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        textField = new JTextField();
        button = new JButton();

        //======== this ========
        setBackground(new Color(0xf2f2f2));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- textField ----
        textField.setHorizontalAlignment(SwingConstants.LEFT);
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                textFieldKeyPressed(e);
            }
            @Override
            public void keyReleased(KeyEvent e) {
                textFieldKeyReleased(e);
            }
        });
        contentPane.add(textField);
        textField.setBounds(95, 70, 200, 70);

        //---- button ----
        button.setText("\u6309\u94ae\uff1a\u70b9\u51fb\u6b64\u5904");
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                buttonMouseClicked(e);
            }
        });
        contentPane.add(button);
        button.setBounds(new Rectangle(new Point(155, 175), button.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(485, 365));
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField textField;
    private JButton button;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
